package com.ccs.transmittal;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

public class Admin_Transmittal_List {

	static
	Misc_Transmittal mt = new Misc_Transmittal();
	
	public void verify_table_header_transmittal_list(WebDriver driver){
		
		String tableHeader[] = { "Corporate/HMO Account:", "Transmittal No.", "Transmittal Date", "Availment Date", "Amount(Php)", "Status", "Action" };
		
		String ca = driver.findElement(By.xpath("//table[@id='data']/thead/tr/th[1]")).getText();
		String tn = driver.findElement(By.xpath("//table[@id='data']/thead/tr/th[2]")).getText();
		String td = driver.findElement(By.xpath("//table[@id='data']/thead/tr/th[3]")).getText();
		String ad = driver.findElement(By.xpath("//table[@id='data']/thead/tr/th[4]")).getText();
		String ap = driver.findElement(By.xpath("//table[@id='data']/thead/tr/th[5]")).getText();
		String s = driver.findElement(By.xpath("//table[@id='data']/thead/tr/th[6]")).getText();
		String a = driver.findElement(By.xpath("//table[@id='data']/thead/tr/th[7]")).getText();
		
		String tableText[] = { ca, tn, td, ad, ap, s, a };
		
		for(int x=0; x<tableText.length ;x++){
			if(!tableHeader[x].equals(tableText[x])){
				System.out.println(tableHeader[x] +" is not equals: " +tableText[x]);
			}
		}
		System.out.println("Table Header of Transmittal List is Correct");
	}
	public void search_transmittal_list(WebDriver driver, String transNo, String option, String hmo){
		//TODO
		//This code will come after transmittal has been made
		//IF Transmittal is DRAFT or FINAL
		driver.findElement(By.xpath("//input[@id='slcGuarantor']")).clear();
		driver.findElement(By.xpath("//input[@id='slcGuarantor']")).sendKeys(hmo);
		driver.findElement(By.xpath("//input[@id='txtTransmittalNo']")).clear();
		driver.findElement(By.xpath("//input[@id='txtTransmittalNo']")).sendKeys(transNo);
		Select status = new Select(driver.findElement(By.xpath("//select[@id='slcStatus']")));
		
		if(option.equals("draft")){
			status.selectByVisibleText("DRAFT");
		}else{ status.selectByVisibleText("FINAL"); }
        
        driver.findElement(By.xpath("//button[contains(., 'Search')]")).click();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        
        List<WebElement> row = driver.findElements(By.xpath("//table[@id='data']/tbody/tr"));
		int size = row.size();
        System.out.println("Total number for Searched Transmittal: "+size);
        
        if(option.equals("draft")){
        	String buttonText = driver.findElement(By.xpath("//table[@id='data']/tbody/tr/td[7]/div/button")).getText();
        	if(buttonText.equals("EDIT")){
				System.out.println("Button Text of Draft Transmittal is :"+buttonText+ ". CORRECT");
			}else{System.out.println("Button Text of Draft Transmittal is :"+buttonText+ ". INCORRECT");}
        	driver.findElement(By.xpath("//table[@id='data']/tbody/tr/td[7]/div/button")).click();
        }else{ 
			String buttonText = driver.findElement(By.xpath("//table[@id='data']/tbody/tr/td[7]/button")).getText();
			if(buttonText.equals("VIEW")){
				System.out.println("Button Text of Final Transmittal is :"+buttonText+ ". CORRECT");
			}else{System.out.println("Button Text of Final Transmittal is :"+buttonText+ ". INCORRECT");}
			driver.findElement(By.xpath("//table[@id='data']/tbody/tr/td[7]/button")).click();
		}
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

	}
	public String date(){
		DateFormat cd = new SimpleDateFormat("MM/dd/yyyy");
        Date current = Calendar.getInstance().getTime();
		String dateToday = cd.format(current);
		return dateToday;
	}
	public void current_date_picker(WebDriver driver){
		
		//Get current date
		String currentDate = date();
		String new_loa_date[] = currentDate.split("/"); 			//split date
		int intDay= Integer.parseInt(new_loa_date[1]);
		
    	String stringDays = Integer.toString(intDay);
    	if(intDay>=1 && intDay<=6){
 		    try{
 		    	driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/table/tbody/tr[1]/td/a[contains(.,'"+stringDays+"')]")).click();
 		    }catch(Exception e){
 		    	driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/table/tbody/tr[2]/td/a[contains(.,'"+stringDays+"')]")).click();
 		    }
    	}else if(intDay>=26 && intDay<=31){
 		    try{
 		    	driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/table/tbody/tr[5]/td/a[contains(.,'"+stringDays+"')]")).click();
 		    }catch(Exception e){
 		    	driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/table/tbody/tr[6]/td/a[contains(.,'"+stringDays+"')]")).click();
 		    }
    	}else{
    		driver.findElement(By.xpath("//a[contains(.,'"+stringDays+"')]")).click();
    	}
    	driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
	}
	public void manual_date_picker(WebDriver driver, String date){
		
		//Set manual date
		String new_loa_date[] = date.split("/"); 			//split date
		int intDay= Integer.parseInt(new_loa_date[1]);
		String array_month[] = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
		Select calendarMonth = new Select(driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/div/div/select[1]")));
		int month = Integer.parseInt(new_loa_date[0]);
		calendarMonth.selectByVisibleText(array_month[month]);
    	String stringDays = Integer.toString(intDay);
    	if(intDay>=1 && intDay<=6){
 		    try{
 		    	driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/table/tbody/tr[1]/td/a[.='"+stringDays+"']")).click();
 		    }catch(Exception e){
 		    	driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/table/tbody/tr[2]/td/a[.='"+stringDays+"']")).click();
 		    }
    	}else if(intDay>=26 && intDay<=31){
 		    try{
 		    	driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/table/tbody/tr[5]/td/a[.='"+stringDays+"']")).click();
 		    }catch(Exception e){
 		    	driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/table/tbody/tr[6]/td/a[.='"+stringDays+"']")).click();
 		    }
    	}else{
    		driver.findElement(By.xpath("//a[contains(.,'"+stringDays+"')]")).click();
    	}
    	driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
	}
	public void yesterday_date_picker(WebDriver driver){
		
        //Transmittal Date Picker
		String dateToday = date();
		System.out.println(dateToday);
        String new_loa_date[] = dateToday.split("/"); 			//split date
        int intDay= Integer.parseInt(new_loa_date[1]);
		
        if(intDay>1){
        	intDay--;
        	String stringDays = Integer.toString(intDay);
        	if(intDay>=1 && intDay<=6){
     		    try{
     		    	driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/table/tbody/tr[1]/td/a[contains(.,'"+stringDays+"')]")).click();
     		    }catch(Exception e){
     		    	driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/table/tbody/tr[2]/td/a[contains(.,'"+stringDays+"')]")).click();
     		    }
        	}else if(intDay>=26 && intDay<=31){
     		    try{
     		    	driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/table/tbody/tr[5]/td/a[contains(.,'"+stringDays+"')]")).click();
     		    }catch(Exception e){
     		    	driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/table/tbody/tr[6]/td/a[contains(.,'"+stringDays+"')]")).click();
     		    }
        	}else{
        		driver.findElement(By.xpath("//a[contains(.,'"+stringDays+"')]")).click();
        	}
        	driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        }else{
        	//This code section is for first day of the month
        	int intMonth = Integer.parseInt(new_loa_date[0]);
        	intMonth--;
        	Select calendarMonth = new Select(driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/div/div/select[1]")));
        	String array_month[] = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
        	System.out.println("New Month "+intMonth);
        	//Current Date is January 1 of the year
        	if(intMonth==0){
        		int intYear = Integer.parseInt(new_loa_date[2]);
        		intYear--;
        		Select calendarYear = new Select(driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/div/div/select[2]")));
        		String stringYear = Integer.toString(intYear);
        		calendarYear.selectByVisibleText(stringYear);
        		calendarMonth.selectByVisibleText("Dec");
        	}else{
        		calendarMonth.selectByVisibleText(array_month[intMonth]);
        	}
        	
        	//NOTE: Leap Year is not included in the code
        	if(array_month[intMonth].equals("Jan") ||
        	   array_month[intMonth].equals("Mar") ||
        	   array_month[intMonth].equals("May") ||	
        	   array_month[intMonth].equals("Jul") ||
        	   array_month[intMonth].equals("Aug") ||
        	   array_month[intMonth].equals("Oct") ||
        	   array_month[intMonth].equals("Dec")){
        	   try{
        		   driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/table/tbody/tr[5]/td/a[contains(.,'31')]")).click();
        	   }catch(Exception e){ driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/table/tbody/tr[6]/td/a[contains(.,'31')]")).click(); }
        	}else if(array_month[intMonth].equals("Apr") ||
        			 array_month[intMonth].equals("Jun") ||
        			 array_month[intMonth].equals("Sep") ||
        			 array_month[intMonth].equals("Nov") ){
         	   try{
        		   driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/table/tbody/tr[5]/td/a[contains(.,'30')]")).click();
        	   }catch(Exception e){ driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/table/tbody/tr[6]/td/a[contains(.,'30')]")).click(); }
        	}else{
         	   try{
        		   driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/table/tbody/tr[5]/td/a[contains(.,'28')]")).click();
        	   }catch(Exception e){ driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/table/tbody/tr[6]/td/a[contains(.,'28')]")).click(); }
        	}
        	
        }
		
	}
	public void check_transmittal_amount_details(WebDriver driver){
		//FINAL or DRAFT Transmittal Verification
		String getAmount = driver.findElement(By.xpath("//div[@id='collapseOne']/div/div/table/tbody/tr/td[6]")).getText();
		if(getAmount.equals("4,995.18")){
			System.out.println("Amount of inpatient is: " +getAmount+" . CORRECT");
		}else{ System.out.println("Amount of inpatient is: " +getAmount+" . INCORRECT"); }
		
		List<WebElement> outpatient = driver.findElements(By.xpath("//div[@id='collapseTwo']/div/div/table/tbody/tr"));
		for(int x=1; x<=outpatient.size() ;x++){
			String getSoa = driver.findElement(By.xpath("//div[@id='collapseTwo']/div/div/table/tbody/tr["+x+"]/td[2]")).getText();
			if(getSoa.contains("PH")){
				String getAmount1 = driver.findElement(By.xpath("//div[@id='collapseTwo']/div/div/table/tbody/tr["+x+"]/td[5]")).getText();
				if(getAmount1.equals("74.97")){
					System.out.println("Amount of outpatient(PHARMACY) is: " +getAmount1+" . CORRECT");
				}else{ System.out.println("Amount of outpatient(PHARMACY) is: " +getAmount1+" . INCORRECT"); }
			}else if(getSoa.contains("DA")){
				String getAmount1 = driver.findElement(By.xpath("//div[@id='collapseTwo']/div/div/table/tbody/tr["+x+"]/td[5]")).getText();
				if(getAmount1.equals("421.06")){
					System.out.println("Amount of outpatient(DASOSS) is: " +getAmount1+" . CORRECT");
				}else{ System.out.println("Amount of outpatient(DASOSS) is: " +getAmount1+" . INCORRECT"); }
			}else{
				String getAmount1 = driver.findElement(By.xpath("//div[@id='collapseTwo']/div/div/table/tbody/tr["+x+"]/td[5]")).getText();
				if(getAmount1.equals("1,494.90")){
					System.out.println("Amount of outpatient(SPU - OR) is: " +getAmount1+" . CORRECT");
				}else{ System.out.println("Amount of outpatient(SPU - OR) is: " +getAmount1+" . INCORRECT"); }
			}
		}
		
		driver.findElement(By.xpath("//li[@id='pfTab']/a/b")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		String getAmount2 = driver.findElement(By.xpath("//div[@id='collapseOnes']/div/div/table/tbody/tr/td[7]")).getText();
		if(getAmount2.equals("1,000.00")){
			System.out.println("Amount of inpatient PF is: " +getAmount2+" . CORRECT");
		}else{ System.out.println("Amount of inpatient PF is: " +getAmount2+" . INCORRECT"); }
		
		String getAmount3 = driver.findElement(By.xpath("//div[@id='collapseTwos']/div/div/table/tbody/tr/td[6]")).getText();
		if(getAmount3.equals("1,000.00")){
			System.out.println("Amount of outpatient(SPU - OR) PF is: " +getAmount3+" . CORRECT");
		}else{ System.out.println("Amount of outpatient(SPU - OR) PF is: " +getAmount3+" . INCORRECT"); }
		
		driver.findElement(By.xpath("//li[@id='hbTab']/a/b")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
	}	
	public void search_param_transmittal_list(WebDriver driver){
		
		String myTableText[] = mt.get_table_details_inpatient_hospital(driver, 1, "final");
		transmittal_search_parameters(driver, "SOA NUMBER", myTableText[1]);
		transmittal_search_parameters(driver, "CHARGE INVOICE", myTableText[1]);
		transmittal_search_parameters(driver, "VISIT NUMBER", myTableText[0]);
		transmittal_search_parameters(driver, "LAST NAME", "COCOLIFE");
		//transmittal_search_parameters(driver, "PIN", "1406065048");
	}
	public void transmittal_search_parameters(WebDriver driver, String optionParam, String value){

		driver.findElement(By.xpath("//div[@id='editFormDiv']/div[1]/button[2]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		Select searchParam = new Select(driver.findElement(By.xpath("//select[@id='searchParamSel']")));
		searchParam.selectByVisibleText(optionParam);
		
		driver.findElement(By.xpath("//input[@id='searchParamInput']")).sendKeys(value);
		driver.findElement(By.xpath("//button[@id='btnSearchParam']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		String style = driver.findElement(By.xpath("//div[@id='collapseOne']/div/div/table/tbody/tr/td[1]")).getAttribute("style");
		//System.out.println(style);
		if(style.contains("background-color: yellow;")){
			System.out.println("Search " +optionParam+" is highlighted. CORRECT"); 
		}else{ System.out.println("Search " +optionParam+" is not highlighted. INCORRECT");  }
		
	}
	public void preview_transmittal_memo(WebDriver driver){
		
		driver.findElement(By.xpath("//form[@id='searchForm']/button")).click();
		driver.findElement(By.xpath("//button[@id='yesBtnDialog']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

	}
	public void visit_not_transmitted_patient(WebDriver driver){
		mt.go_to_admin_home(driver);
		mt.go_to_transmittal_admin(driver);
		driver.findElement(By.xpath("//form[@id='transmittalForm']/a/strong")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		String error = driver.findElement(By.xpath("//span[@id='spanAlertMessageList']")).getText();
		if(error.equals("Please enter Corporate/HMO Account.")){
			System.out.println(error+ " .CORRECT");
		}else{ System.out.println(error+ " .INCORRECT");}
		driver.findElement(By.xpath("//input[@id='slcGuarantor']")).sendKeys("COCOLIFE HEALTH CARE");
		driver.findElement(By.xpath("//form[@id='transmittalForm']/a/strong")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
	}
	public void include_not_transmitted_patient(WebDriver driver, String type){
		
		String newInVisitNo[] = Admin_Create_Transmittal_List.inVisitNo;
		String newOutVisitNo[] = Admin_Create_Transmittal_List.outVisitNo;
		System.out.println(Arrays.toString(newInVisitNo));
		System.out.println(Arrays.toString(newOutVisitNo));
		
		if(type.equals("inpatient")){
			for(int x =0; x<newInVisitNo.length;x++){
				driver.findElement(By.xpath("//html/body/div[1]/div[5]/button[1]")).click();
				driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
				WebElement textSearch = driver.findElement(By.xpath("//input[@id='transmittalSearchText']"));
				textSearch.sendKeys(newInVisitNo[x]);
				
				Actions actions = new Actions(driver);
		        WebElement mouseover = driver.findElement(By.xpath("//div[@id='addTransmittalDialog']/div/div/div[1]/button/i"));
		        actions.moveToElement(mouseover);
		        actions.click().perform();
		        driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
	
				driver.findElement(By.xpath("//input[@id='chkAllNonTrans']")).click();
				driver.findElement(By.xpath("//button[contains(.,'Submit')]")).click();
				driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			}
		}else{
			for(int x =0; x<newOutVisitNo.length;x++){
				driver.findElement(By.xpath("//html/body/div[1]/div[5]/button[1]")).click();
				driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
				WebElement textSearch = driver.findElement(By.xpath("//input[@id='transmittalSearchText']"));
				textSearch.sendKeys(newOutVisitNo[x]);

				Actions actions = new Actions(driver);
		        WebElement mouseover = driver.findElement(By.xpath("//div[@id='addTransmittalDialog']/div/div/div[1]/button/i"));
		        actions.moveToElement(mouseover);
		        actions.click().perform();
		        driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
				
				driver.findElement(By.xpath("//input[@id='chkAllNonTrans']")).click();
				driver.findElement(By.xpath("//button[contains(.,'Submit')]")).click();
				driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
			}
		}
	}
	public void exclude_not_transmitted_patient(WebDriver driver, String type){
		
		if(type.equals("inpatient")){
			String checker = driver.findElement(By.xpath("//input[@id='chkAllTrans']")).getAttribute("checked");
			System.out.println("Inpatient is:" +checker);
			if(checker.equals("true")){
				driver.findElement(By.xpath("//input[@id='chkAllTrans']")).click();
			}
		}
		if(type.equals("outpatient")){
			String checker = driver.findElement(By.xpath("//input[@id='chkAllTransOut']")).getAttribute("checked");
			System.out.println("Outpatient is:" +checker);
			if(checker.equals("true")){
				driver.findElement(By.xpath("//input[@id='chkAllTransOut']")).click();
			}
		}
		/*
		driver.findElement(By.xpath("//button[contains(.,'Save as Draft')]")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//button[@id='yesBtnDialog']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		 */
	}
	public void verify_include_not_transmitted_patient(WebDriver driver, String type, String process){
		
		List<WebElement> inVisitNum = driver.findElements(By.xpath("//div[@id='collapseOne']/div/div/table/tbody/tr"));
		int sizeIn = inVisitNum.size();
		List<WebElement> outVisitNum = driver.findElements(By.xpath("//div[@id='collapseTwo']/div/div/table/tbody/tr"));
		int sizeOut = outVisitNum.size();
		
		boolean getCheck=true;
		if(type.equals("inpatient")){
			String text = driver.findElement(By.xpath("//div[@id='collapseOne']/div/div/table/tbody/tr/td[1]")).getText();
			getCheck = mt.checkIt(text);
			System.out.println("Size of " +type+ " row: "+sizeIn);
		}else{
			String text = driver.findElement(By.xpath("//div[@id='collapseTwo']/div/div/table/tbody/tr/td[1]")).getText();
			getCheck = mt.checkIt(text);
			System.out.println("Size of " +type+ " row: "+sizeOut);
		}
		if(process.contains("validate")){
			if(getCheck==true){
				System.out.println("Size of " +type+ " is Zero. CORRECT");
			}else{
				System.out.println("Size of " +type+ " is not Zero. INCORRECT");
			}
		}else
		{
			String newInVisitNo[] = Admin_Create_Transmittal_List.inVisitNo;
			String newOutVisitNo[] = Admin_Create_Transmittal_List.outVisitNo;
			
			if(type.equals("inpatient")){
				if(newInVisitNo.length==sizeIn){
					System.out.println("Size of " +type+ " is CORRECT");
				}else
				{
					System.out.println("Size of " +type+ " is INCORRECT");
				}
			}else{
				if(newOutVisitNo.length==sizeOut){
					System.out.println("Size of " +type+ " is CORRECT");
				}else
				{
					System.out.println("Size of " +type+ " is INCORRECT");
				}
			}
		}
	}
	public void check_unopened_transmittal(WebDriver driver){
		String text = driver.findElement(By.xpath("//table[@id='data']/tbody/tr/td[7]/font")).getText();
		if(text.contains("Unopened")){
			System.out.println("Final Transmittal has: "+text+" .CORRECT");
		}else{
			System.out.println("Final Transmittal has: "+text+" .INCORRECT");
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
